package com.example.tallybook.data

import kotlinx.coroutines.flow.Flow
import java.time.LocalDateTime
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TransactionRepository @Inject constructor(
    private val transactionDao: TransactionDao
) {
    fun getAllTransactions(): Flow<List<Transaction>> = transactionDao.getAllTransactions()

    fun getTransactionsByType(type: TransactionType): Flow<List<Transaction>> = 
        transactionDao.getTransactionsByType(type)

    fun getTransactionsByDateRange(startDate: LocalDateTime, endDate: LocalDateTime): Flow<List<Transaction>> =
        transactionDao.getTransactionsByDateRange(startDate, endDate)

    fun getTotalIncome(): Flow<Double?> = transactionDao.getTotalIncome()

    fun getTotalExpense(): Flow<Double?> = transactionDao.getTotalExpense()

    fun searchTransactions(query: String): Flow<List<Transaction>> = 
        transactionDao.searchTransactions(query)

    suspend fun insertTransaction(transaction: Transaction) = 
        transactionDao.insertTransaction(transaction)

    suspend fun updateTransaction(transaction: Transaction) = 
        transactionDao.updateTransaction(transaction)

    suspend fun deleteTransaction(transaction: Transaction) = 
        transactionDao.deleteTransaction(transaction)

    suspend fun deleteTransactionById(id: Long) = 
        transactionDao.deleteTransactionById(id)

    suspend fun insertSampleData() {
        val sampleTransactions = SampleData.getSampleTransactions()
        sampleTransactions.forEach { transaction ->
            transactionDao.insertTransaction(transaction)
        }
    }
} 